﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using SchoolProject2012.Areas.Shared.Models;
using SchoolProject2012.Functions;
using SchoolProject2012.Langue;

namespace SchoolProject2012.Areas.Shared.Controllers
{
    public class logController : Controller
    {
        private SchoolProjectBddEntities db = new SchoolProjectBddEntities();
        //
        // GET: /log/

        [AllowAnonymous]
        public ActionResult Connexion(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            if (Session["login"] != null)
            {
                return View("Forbidden");
            }
            return View();
        }

        [AllowAnonymous]
        [HttpPost]
        public ActionResult Connexion(string langue, PersonneView personne)
        {
            if (ModelState.IsValid)
            {
                personne.MDP = Security.getHash(personne.MDP);
                if (Membership.ValidateUser(personne.LOGIN, personne.MDP))
                {
                    FormsAuthentication.SetAuthCookie(personne.LOGIN, true);
                    string role = db.PersonneView.SingleOrDefault(pers => pers.LOGIN == personne.LOGIN && pers.MDP == personne.MDP).ROLE;
                    bool rep = Roles.IsUserInRole(personne.LOGIN, role);
                    if (!rep)
                    {
                        Roles.AddUserToRole(personne.LOGIN, role);
                    }
                    Session.Add("login", personne.LOGIN);
                    Session.Timeout = 10;
                    return RedirectToAction("index", "home", new { area = role, langue = langue });
                }
                else
                {
                    ViewData["error"] = "Erreur d'identification";
                }
            }
            return View();
        }

        [Authorize]
        public ActionResult Deconnexion(string langue)
        {
            FormsAuthentication.SignOut();
            Session.Remove("login");
            return RedirectToAction("Index", "home", new { area = "Shared", langue = langue });
        }

        [AllowAnonymous]
        public ViewResult Password(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            return View();
        }

        [AllowAnonymous]
        [HttpPost]
        public ActionResult Password(string langue, string login)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            try
            {
                if (ModelState.IsValid)
                {
                    var p = db.PersonneView.Single(x => x.LOGIN == login);
                    if (p != null)
                    {
                        string adr = p.EMAIL;
                        string message = Mail.messagePassword(adr);
                        List<string> to = new List<string>();
                        to.Add(adr);
                        Mail.sendMail(Resources.mdpForget, "administration@ingesup.com", to, message);
                    }
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    return View();
                }
            }
            catch(Exception)
            {
                return View("error");
            }
        }

        [AllowAnonymous]
        public ActionResult confirmPassword(string email)
        {
            try
            {
                var p = db.PersonneView.Single(x => x.EMAIL == email);
                double mdp = DateTime.Now.ToOADate();
                string message = Mail.messageConfirmPassword(mdp.ToString());
                List<string> to = new List<string>();
                to.Add(email);
                Mail.sendMail(Resources.mdpForget, "administration@ingesup.com", to, message);
                if (p.ROLE == "Admin")
                {
                    var user = db.ADMINISTRATEUR.Single(x => x.MAILPERS == email);
                    user.MDPPERS = Security.getHash(mdp.ToString());
                    db.Refresh(System.Data.Objects.RefreshMode.StoreWins, user);
                }
                if (p.ROLE == "Service-pedagogique")
                {
                    var user = db.SERVICEPEDAGOGIQUE.Single(x => x.MAILPERS == email);
                    user.MDPPERS = Security.getHash(mdp.ToString());
                    db.Refresh(System.Data.Objects.RefreshMode.StoreWins, user);
                }
                if (p.ROLE == "Eleve")
                {
                    var user = db.ELEVE.Single(x => x.MAILPERS == email);
                    user.MDPPERS = Security.getHash(mdp.ToString());
                    db.Refresh(System.Data.Objects.RefreshMode.StoreWins, user);
                }
                if (p.ROLE == "Professeur")
                {
                    var user = db.PROFESSEUR.Single(x => x.MAILPERS == email);
                    user.MDPPERS = Security.getHash(mdp.ToString());
                    db.Refresh(System.Data.Objects.RefreshMode.StoreWins, user);
                }
                if (p.ROLE == "Charge-de-placement")
                {
                    var user = db.CHARGEDEPLACEMENT.Single(x => x.MAILPERS == email);
                    user.MDPPERS = Security.getHash(mdp.ToString());
                    db.Refresh(System.Data.Objects.RefreshMode.StoreWins, user);
                }
                db.SaveChanges();
                return RedirectToAction("Index", "Home");
            }
            catch (Exception)
            {
                return View("error");
            }
        }
    }
}
